package org.gruposp2p.aula.gwt.client.event;

import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;

public class AulaEventBus {
	
	private static AulaEventBus instance;
	private HandlerManager handlerManager;

	private AulaEventBus () {
		handlerManager = new HandlerManager (this);
	}
	
	/**
	 * @return the instance of the class
	 */
	public static AulaEventBus getInstance() {
		if (instance == null) {
			instance = new AulaEventBus();
		}
		return instance;
	}

	/**
	 * @param handler the handler of the event
	 */
	public void registerToCourseChange(final CourseChangeHandler handler) {
		handlerManager.addHandler(CourseChangeEvent.getType(), 
			new CourseChangeHandler () {
				@Override
				public void processCourse(CourseChangeEvent event) {
					handler.processCourse(event);	
				}
		});
	}
	
	/**
	 * @param handler the handler of the event
	 */
	public void registerToCoursegroupChange(final CoursegroupChangeHandler handler) {
		handlerManager.addHandler(CoursegroupChangeEvent.getType(), 
			new CoursegroupChangeHandler () {
				@Override
				public void processCoursegroup(CoursegroupChangeEvent event) {
					handler.processCoursegroup(event);	
				}
		});
	}	
	
	/**
	 * @param handler the handler of the event
	 */
	public void registerToCoursesChange(final CoursesChangeHandler handler) {
		handlerManager.addHandler(CoursesChangeEvent.getType(), 
			new CoursesChangeHandler () {
				@Override
				public void processCourses(CoursesChangeEvent event) {
					handler.processCourses(event);	
				}
		});
	}
	
	/**
	 * @param handler the handler of the event
	 */
	public void registerToStudentChange(final StudentChangeHandler handler) {
		handlerManager.addHandler(StudentChangeEvent.getType(), 
			new StudentChangeHandler () {
				@Override
				public void processStudent(StudentChangeEvent event) {
					handler.processStudent(event);					
				}
		});
	}	
	
	/**
	 * Fire an event.
	 */
	 public void fireEvent(GwtEvent event){
		 handlerManager.fireEvent(event);
     }

	
}
